package com.yimicloud.common.svn;

import com.yimicloud.common.lang.StringUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Justin on 2015/9/7.
 */
public class AuthzUtil {

    public AuthzUtil(){

    }

    public static void main(String[] args) throws IOException {
        String appName = "TestMaven";
        ArrayList list = new ArrayList();
        String path = "E:/svndemo/conf/authz";
        SvnUser usersItems = new SvnUser();
        usersItems.setUserName("caozhi");
        SvnUser usersItems1 = new SvnUser();
        usersItems1.setUserName("wjs");
        list.add(usersItems);
        list.add(usersItems1);
        AuthzUtil authzUntils = new AuthzUtil();
        String content = authzUntils.addAuthz(path, list, appName);
        if(StringUtil.isNotEmpty(content)) {
            authzUntils.writeAddAuthz(path, content, appName);
        }

    }

    public String addAuthz(String path, List<SvnUser> list, String appName) throws IOException {
        System.out.println("svnAuthz权限路径为=======>" + path);
        BufferedReader br = null;
        String line = null;
        StringBuffer buf = new StringBuffer();

        try {
            System.out.println(list.size());
            if(list != null && list.size() > 0) {
                br = new BufferedReader(new FileReader(path));

                while(true) {
                    while((line = br.readLine()) != null) {
                        System.out.println(line);
                        if(line.startsWith("[groups]")) {
                            buf.append("[groups]");
                            buf.append("\n");
                            String e = new String();

                            SvnUser items;
                            for(Iterator var9 = list.iterator(); var9.hasNext(); e = e + items.getUserName() + ",") {
                                items = (SvnUser)var9.next();
                            }

                            System.out.println(e);
                            buf.append("group_" + appName + " = " + e.substring(0, e.length() - 1));
                            buf.append("\n");
                        } else {
                            buf.append(line);
                            buf.append("\n");
                        }
                    }

                    return buf.toString();
                }
            }
        } catch (Exception var18) {
            var18.printStackTrace();
        } finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException var17) {
                    br = null;
                }
            }

        }

        return buf.toString();
    }

    public void writeAddAuthz(String path, String content, String appName) {
        System.out.println("svnAuthz权限路径为=======>" + path);
        BufferedWriter bw = null;

        try {
            StringBuffer e = new StringBuffer(content);
            e.append("\n");
            e.append("[/" + appName + "]");
            e.append("\n");
            e.append("@group_" + appName + " = rw");
            bw = new BufferedWriter(new FileWriter(path));
            bw.write(e.toString());
        } catch (Exception var14) {
            var14.printStackTrace();
        } finally {
            if(bw != null) {
                try {
                    bw.close();
                } catch (IOException var13) {
                    bw = null;
                }
            }

        }

    }

    public String updateAuthz(String path, List<SvnUser> list, String appName) throws IOException {
        System.out.println("svnAuthz权限路径为=======>" + path);
        BufferedReader br = null;
        String line = null;
        StringBuffer buf = new StringBuffer();

        try {
            if(list != null && list.size() > 0) {
                br = new BufferedReader(new FileReader(path));

                while(true) {
                    while((line = br.readLine()) != null) {
                        if(line.startsWith("[groups]")) {
                            buf.append("[groups]");
                            buf.append("\n");
                            String e = new String();

                            SvnUser items;
                            for(Iterator var9 = list.iterator(); var9.hasNext(); e = e + items.getUserName() + ",") {
                                items = (SvnUser)var9.next();
                            }

                            buf.append("group_" + appName + " = " + e.substring(0, e.length() - 1));
                            buf.append("\n");
                        } else if(!line.startsWith("group_" + appName)) {
                            buf.append(line);
                            buf.append("\n");
                        }
                    }

                    return buf.toString();
                }
            }
        } catch (Exception var18) {
            var18.printStackTrace();
        } finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException var17) {
                    br = null;
                }
            }

        }

        return buf.toString();
    }

    public String deleteAuthz(String path, String appName) throws IOException {
        System.out.println("svnAuthz权限路径为=======>" + path);
        BufferedReader br = null;
        String line = null;
        StringBuffer buf = new StringBuffer();

        try {
            br = new BufferedReader(new FileReader(path));

            while((line = br.readLine()) != null) {
                if(!line.startsWith("group_" + appName) && line.indexOf(appName) < 0) {
                    buf.append(line);
                    buf.append("\n");
                }
            }
        } catch (Exception var15) {
            var15.printStackTrace();
        } finally {
            if(br != null) {
                try {
                    br.close();
                } catch (IOException var14) {
                    br = null;
                }
            }

        }

        return buf.toString();
    }

    public void writeAuthz(String path, String content) {
        System.out.println("svnAuthz权限路径为=======>" + path);
        BufferedWriter bw = null;

        try {
            bw = new BufferedWriter(new FileWriter(path));
            bw.write(content);
        } catch (Exception var13) {
            var13.printStackTrace();
        } finally {
            if(bw != null) {
                try {
                    bw.close();
                } catch (IOException var12) {
                    bw = null;
                }
            }

        }

    }
}
